package com.forezp.datastruct;

/**
 * Created by forezp on 2017/12/5.
 */
public class CircleQuene<T> {

    T[] items;
    private int capcity;
    private int firstIndex;
    private int lastIndex;
    private int size;

    public CircleQuene(int capcity) {
        this.capcity = capcity;
        items = (T[]) new Object[capcity];
    }

    public boolean isFull() {
        return (size == capcity) ? true : false;
    }

    public boolean isEmpty() {
        return (size == 0) ? true : false;
    }

    public boolean insert(T data) {
        if (isFull()) {
            return false;
        }
        items[lastIndex++] = data;
        lastIndex = lastIndex % capcity;
        size++;
        return true;
    }

    public T delete() {
        if (isEmpty()) {
            return null;
        }
        T returnData = items[firstIndex++];
        firstIndex = firstIndex % capcity;
        size--;
        return returnData;
    }

    public void println() {
        for (int i = firstIndex; i < size + firstIndex; i++) {
            System.out.println(items[i%capcity]);
        }

        System.out.println();
        System.out.println();
    }

}
